package com.example.test_activity.MVP.view;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.test_activity.MVP.presenter.IViewPresenter;
import com.example.test_activity.MVP.presenter.ViewPresenter;
import com.example.test_activity.R;
import com.example.test_activity.fragment.CourseFragment;
import com.example.test_activity.fragment.InformationFragment;
import com.example.test_activity.fragment.ScoreEveryFragment;
import com.example.test_activity.fragment.ScoreFragment;
import com.example.test_activity.fragment.SyllabusFragment;
import com.example.test_activity.http.HttpUtil;
import com.example.test_activity.viewUtil.CoordinatorMenu;

import java.util.List;


public class ViewActivity extends FragmentActivity implements IView, View.OnClickListener {

    private CoordinatorMenu mCoordinatorMenu;

    public FragmentTransaction transaction;

    private ScoreFragment scoreFragment;
    private InformationFragment informationFragment;
    private CourseFragment courseFragment;
    private ScoreEveryFragment scoreEveryFragment;
    private SyllabusFragment syllabusFragment;

    private TextView tv_name;
    private TextView tv_number;
    private String user_name;
    private String user_number;

    private IViewPresenter viewPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        init();
        viewPresenter = new ViewPresenter(this);
        String info = getIntent().getStringExtra("user");

        viewPresenter.reviseString(info);
        initFragment(0);
    }

    private void init() {
        mCoordinatorMenu = (CoordinatorMenu) findViewById(R.id.menu);

        findViewById(R.id.text_1).setOnClickListener(this);
        findViewById(R.id.text_2).setOnClickListener(this);
        findViewById(R.id.text_3).setOnClickListener(this);
        findViewById(R.id.text_4).setOnClickListener(this);
        findViewById(R.id.text_5).setOnClickListener(this);
        findViewById(R.id.logout).setOnClickListener(this);
        findViewById(R.id.exit).setOnClickListener(this);

        tv_name = (TextView) findViewById(R.id.user_name);
        tv_number = (TextView) findViewById(R.id.user_info);
    }

    @Override
    public void setName(List<String> list) {
        user_name = list.get(0);
        user_number = list.get(1);

        tv_name.setText(user_name);
        tv_number.setText(user_number);
    }

    private void initFragment(int index) {
        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        hideFragment(transaction);

        switch (index) {
            case 0:
                if (syllabusFragment == null) {
                    syllabusFragment= new SyllabusFragment(user_number);
                    transaction.add(R.id.context, syllabusFragment);
                } else {
                    transaction.show(syllabusFragment);
                }

                break;
            case 1:
                if (scoreFragment == null) {
                    scoreFragment = new ScoreFragment();
                    transaction.add(R.id.context, scoreFragment);
                } else {
                    transaction.show(scoreFragment);
                }

                break;

            case 2:
                if (informationFragment == null) {
                    informationFragment = new InformationFragment();
                    transaction.add(R.id.context, informationFragment);
                } else {
                    transaction.show(informationFragment);
                }

                break;
            case 3:
                if (courseFragment == null) {
                    courseFragment = new CourseFragment();
                    transaction.add(R.id.context, courseFragment);
                } else {
                    transaction.show(courseFragment);
                }
                break;
            case 4:
                if (scoreEveryFragment == null) {
                    scoreEveryFragment = new ScoreEveryFragment();
                    transaction.add(R.id.context, scoreEveryFragment);
                } else {
                    transaction.show(scoreEveryFragment);
                }
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (scoreFragment != null) {
            transaction.hide(scoreFragment);
        }
        if (syllabusFragment != null) {
            transaction.hide(syllabusFragment);
        }
        if (informationFragment != null) {
            transaction.hide(informationFragment);
        }
        if (courseFragment != null) {
            transaction.hide(courseFragment);
        }
        if (scoreEveryFragment != null) {
            transaction.hide(scoreEveryFragment);
        }
    }

    @Override
    public void onBackPressed() {
        if (mCoordinatorMenu.isOpened()) {
            mCoordinatorMenu.closeMenu();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        mCoordinatorMenu.closeMenu();
        switch (v.getId()) {
            case R.id.text_1:
                initFragment(0);
                break;
            case R.id.text_2:
                initFragment(1);
                break;
            case R.id.text_3:
                initFragment(2);
                break;
            case R.id.text_4:
                initFragment(3);
                break;
            case R.id.text_5:
                initFragment(4);
                break;
            case R.id.logout:
                viewPresenter.doMethod("logout");
                break;
            case R.id.exit:
                viewPresenter.doMethod("exit");
                break;
        }
    }

    @Override
    public void exit() {
        finish();
    }

    @Override
    public void logout() {
        Intent intent = new Intent(ViewActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
